<template>
  <div class="login-container">
    <el-card class="login-box">
      <h2 class="title">企业数据分析平台</h2>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            @change="handlePhoneChange"
          >
            <template #prefix>
              <el-icon><Phone /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="form.code"
            placeholder="请输入验证码"
            :maxlength="6"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
            <template #append>
              <el-button 
                type="primary" 
                @click="getCode" 
                :disabled="countdown > 0"
              >
                {{ countdown ? `${countdown}s` : '获取验证码' }}
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-button 
          type="primary" 
          class="login-btn" 
          :loading="loading"
          @click="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Phone, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const formRef = ref()
const loading = ref(false)
const countdown = ref(0)

const form = ref({
  phone: '',
  code: ''
})

const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码长度必须为6位', trigger: 'blur' }
  ]
}

const handlePhoneChange = (value: string) => {
  if (value.length === 11) {
    form.value.code = value.slice(-6)
  }
}

const getCode = () => {
  if (!form.value.phone) {
    ElMessage.warning('请先输入手机号')
    return
  }
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) clearInterval(timer)
  }, 1000)
}

const handleLogin = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true
      setTimeout(() => {
        localStorage.setItem('userInfo', JSON.stringify(form.value))
        localStorage.setItem('token', 'mock-token-' + Date.now())
        loading.value = false
        router.push('/dashboard')
        ElMessage.success('登录成功')
      }, 1000)
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
}

.login-box {
  width: 420px;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 40px;
  color: #303133;
  font-size: 28px;
}

.login-btn {
  width: 100%;
  margin-top: 20px;
  height: 40px;
  font-size: 16px;
}

:deep(.el-input-group__append) {
  padding: 0;
  overflow: hidden;
  .el-button {
    margin: 0;
    border-radius: 0;
  }
}
</style>